import axios from "axios";
import { Skynet, LogLevel, LogType } from "@xiaoe/skynet";
import { AD_API_PATH } from "./utils/constants"
import { isTourist } from "./utils/index"

//config可选
export const skynet = new Skynet({
  projectName: "transaction",
  projectVersion: "v1.0.0",
  auto: false,
  url: "https://logreport.xiaoeknow.com/ecommerce/report",
});

skynet.turnOn();

axios.interceptors.response.use(
  function (res) {
    return res.data;
  },
  function (error) {
    return Promise.reject(error);
  }
);

function fetchInfo(url, params, method = "post", headers) {
  let promise;
  let response;
  if (["post", "put", "patch"].includes(method.toLocaleLowerCase())) {
    promise = axios[method](url, params, { headers });
  } else {
    promise = axios[method](url, {
      params,
      headers,
    });
  }
  return promise
    .then((res) => {
      response = res;
      console.log('res----',res);
      // return res
      return res.code === 0 ? res.data : Promise.reject(res);
    })
    .catch((err) => {
      response = err;
      return Promise.reject(err);
    })
    .finally(() => {
      try {
        skynet.report({
          details: {
            req: {
              url,
              method: "POST",
              params,
            },
            res: {
              response,
            },
          },
          level: response?.code === 0 ? LogLevel.INFO : LogLevel.FATAL,
          type: LogType.CUSTOM,
        });
      } catch (error) {
        console.error(error);
      }
    });
}

// 获取支付方式列表
export function fetchPayWay(params,id, type) {
  let apiName = AD_API_PATH.FetchPayWay
  let headerConfig = {
    "Content-Type": "application/json;charset=UTF-8",
  }
  if(isTourist(type)){
    apiName = AD_API_PATH.TouristfetchPayWay
  }
  if(id && isTourist(type)){
    headerConfig = {
      "Content-Type": "application/json;charset=UTF-8",
      "xiaoe-tourist-ident": id
    }
  }
  return fetchInfo(
    apiName,
    {
      bizData: params,
    },
    "post",
    headerConfig
  );
}

/**
 * 批价
 * @param {object} params 批价参数
 * @param {string} id 用户临时身份id
 * @param {string} urlPrefix 接口url前缀，比如/alive
 * @returns promise 请求结果
 */
export function calculateAdPrice(params,id, type) {
  let apiName = AD_API_PATH.CalculateAdPrice
  let headerConfig = {
    "Content-Type": "application/json;charset=UTF-8",
  }
  if(isTourist(type)){
    apiName = AD_API_PATH.TouristCalculateAdPrice
  }
  if(id && isTourist(type)){
    headerConfig = {
      "Content-Type": "application/json;charset=UTF-8",
      "xiaoe-tourist-ident": id
    }
  }
  return fetchInfo(
    apiName,
    {
      bizData: params,
    },
    "post",
    headerConfig
  );
}

/**
 * 下单
 * @param {object} orderParams 下单参数
 * @param {string} urlPrefix 接口url前缀，比如/alive
 * @returns promise 请求结果
 */
export function createAdOrder(params,id, type) {
  let apiName = AD_API_PATH.CreateOrder
  let headerConfig = {
    "Content-Type": "application/json;charset=UTF-8",
  }
  if(isTourist(type)){
    apiName = AD_API_PATH.TouristCreateOrder
  }
  if(id && isTourist(type)){
    headerConfig = {
      "Content-Type": "application/json;charset=UTF-8",
      "xiaoe-tourist-ident": id
    }
  }
  return fetchInfo(
    apiName,
    {
      bizData: params,
    },
    "post",
    headerConfig
  );
}

/**
 * 查询订单
 * @param {object} params 订单查询参数
 * @param {string} urlPrefix 接口url前缀，比如/alive
 * @returns promise 请求结果
 */
export function queryAdOrder(params,id, type) {
  let apiName = AD_API_PATH.QueryOrder
  let headerConfig = {
    "Content-Type": "application/json;charset=UTF-8",
  }
  if(isTourist(type)){
    apiName = AD_API_PATH.TouristqueryOrder
  }
  if(id && isTourist(type)){
    headerConfig = {
      "Content-Type": "application/json;charset=UTF-8",
      "xiaoe-tourist-ident": id
    }
  }
  return fetchInfo(
    apiName,
    {
      bizData: params,
    },
    "post",
    headerConfig
  );
}
